# -*- coding: utf-8 -*-

"""
@Datetime: 2019/5/21
@Author: Zhang Yafei
"""
""" 全局变量 局部变量 闭包变量 """

x = 0


def outer():
    x = 1

    def inner():
        x = 2
        print("inner:", x)

    inner()
    print("outer:", x)


outer()
print("global:", x)

# inner: 2
# outer: 1
# global: 0

""" 闭包修改局部变量应声明nonlocal """
x = 0


def outer():
    x = 1

    def inner():
        nonlocal x
        x = 2
        print("inner:", x)

    inner()
    print("outer:", x)


outer()
print("global:", x)

# inner: 2
# outer: 2
# global: 0

""" 闭包修改全局变量应声明global """
x = 0


def outer():
    x = 1

    def inner():
        global x
        x = 2
        print("inner:", x)

    inner()
    print("outer:", x)


outer()
print("global:", x)

# inner: 2
# outer: 1
# global: 2
